package com.wdl.modules.myhouse.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wdl.modules.myhouse.common.Constant;
import com.wdl.modules.myhouse.common.MyPage;
import com.wdl.modules.myhouse.common.Result;
import com.wdl.modules.myhouse.dto.UploadLogDTO;
import com.wdl.modules.myhouse.entity.FileDescEntity;
import com.wdl.modules.myhouse.entity.FileEntity;
import com.wdl.modules.myhouse.entity.UploadLogEntity;
import com.wdl.modules.myhouse.service.FileService;
import com.wdl.modules.myhouse.service.UploadLogService;
import com.wdl.modules.myhouse.utils.util.PageUtil;
import com.wdl.modules.myhouse.vo.UploadApprovalVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 上传记录表
 *
 * @author lucain sunlightcs@gmail.com
 * @since 1.0.0 2022-10-08
 */
@RestController
@RequestMapping("/uploadLog")
@Api(tags = "上传记录表")
@Slf4j
public class UploadLogController {
    @Autowired
    private UploadLogService uploadLogService;
    @Autowired
    private FileService fileService;


    @GetMapping("/changeState")
    @ApiOperation("修改文件审批状态")
    public Result<Void> changeState(@RequestParam("state") int state, @RequestParam("id") int id) {
        FileEntity fileEntity = new FileEntity();
        FileDescEntity descEntity = new FileDescEntity();
        UploadLogEntity entity = new UploadLogEntity();
        entity.setFileId(fileEntity.getId());
        entity.setSoftState(state);
        entity.setId(id);
        uploadLogService.updateById(entity);
        return new Result<Void>();
    }


    @PostMapping("/save")
    @ApiOperation("添加上传记录")
    public Result<Void> save(@RequestBody UploadLogDTO uploadLogDTO) {
        UploadLogEntity entity = new UploadLogEntity();
        BeanUtils.copyProperties(uploadLogDTO, entity);
        uploadLogService.save(entity);
        return new Result<Void>().ok();
    }

    @GetMapping("/all")
    @ApiOperation("分页获取上传记录")
    public Result<IPage<UploadApprovalVo>> getUploadLogByPage(@NotNull MyPage page) {
        log.info("获取文件上传记录,从{}页开始获取{}条", page.getCurrent(), page.getSize());
        IPage<UploadLogEntity> iPage = PageUtil.getIPage(page, new UploadLogEntity());
        IPage<UploadApprovalVo> page1 = uploadLogService.getUploadLogAndOtherInfo(iPage);
        return new Result<IPage<UploadApprovalVo>>().ok(page1);
    }


}